package cn.edu.tju.ldj.analytics.utils;

import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;

/**
 * 
 * @author cjy
 * 系统工具类，包括错误处理，日志记录，断言处理
 */
public class IUtilSys {
	private static Logger logger;
	
	static {
		logger = Logger.getLogger(IUtilSys.class.getName());
	}
	
	/**
	 * 记录错误
	 * @param t
	 */
	public void error(java.lang.Throwable t){
		logger.error(t);
	}
	/**
	 * 记录日志
	 * @param s
	 */
	public void log(String s){
		logger.info(s);
	}
	
	/**
	 * 执行一个断言，如果断言失败，会抛出异常
	 * @param b
	 */
	public void ASSERT(boolean b){
		if(!b){
			throw new java.lang.RuntimeException("assert fail!");
		}
	}
	private int compare(Object o1,Object o2){
		if(o1 instanceof Integer){
			return ((Integer)o1).compareTo((Integer)o2);
		}else if(o1 instanceof Double){
			return ((Double)o1).compareTo((Double)o2);
		}else if(o1 instanceof String){
			return ((String)o1).compareTo((String)o2);
		}else if(o1 instanceof java.util.Date){
			return ((java.util.Date)o1).compareTo((java.util.Date)o2);
		}else{
			throw new java.lang.IllegalArgumentException("invalid object type:"+o1.getClass());
		}
	}
	/**
	 * 向一个list中插入元素，由小到大的顺序，目前只能处理String
	 * @param list
	 * @param o
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" }) 
	public void insertToList(List list,Object o){
		if(list.contains(o)){
			return;
		}
		int i=list.size()-1;
		
		list.add(null);
		while(i>=0&&(compare(list.get(i),o)>0)){
			list.set(i+1, list.get(i));
			i--;
		}
		list.set(i+1, o);
	}
	
	@SuppressWarnings("rawtypes")
	public static void main(String[] args){
		IUtilSys s=new IUtilSys();
		List l=new Vector();
		s.insertToList(l,"1");
		s.insertToList(l,"5");
		s.insertToList(l,"5");
		s.insertToList(l,"3");
		for(int i=0;i<l.size();i++){
			System.out.println(l.get(i));
		}
	}
}
